package com.qgy.springsecurity.web;

import com.qgy.springsecurity.models.MyResult;
import com.qgy.springsecurity.models.bases.SysUser;
import com.qgy.springsecurity.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/qgy/users")
public class UserWeb {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public MyResult login(@RequestBody SysUser user) {
        if (Objects.isNull(user.getUserName()) || user.getUserName().trim().isEmpty()) return MyResult.error().message("用户名不能为空");
        if (Objects.isNull(user.getPassword()) || user.getPassword().trim().isEmpty()) return MyResult.error().message("密码不能为空");
        return userService.login(user);
    }

    @GetMapping("/getUserInfo")
    public MyResult getUserInfo(@RequestHeader(required = false, value = "Authorization") String token) {
        if (Objects.isNull(token)) return MyResult.error().message("用户未登录");
        if (!token.startsWith("Bearer ")) return MyResult.error().message("token格式错误");
        return userService.getUserInfo(token.substring(7));
    }
}
